package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.utils.AliOssUtil;
import com.itheima.mapper.CheckgroupMapper;
import com.itheima.mapper.SetmealCheckgroupMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealCheckgroupMapper setmealCheckgroupMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private AliOssUtil aliOssUtil;
    @Autowired
    private CheckgroupMapper checkgroupMapper; // 添加校验需要新的Mapper
    /**
     * 新增套餐
     * @param checkgroupIds
     * @param setmeal
     */
    @Transactional
    public void add(List<Integer> checkgroupIds, Setmeal setmeal) {
        //先将套餐数据添加到套餐表中
        setmealMapper.add(setmeal);
        // 将已提交的图片移动到committedImages集合
        moveImageToCommitted(setmeal.getImg());
        //回显套餐id
        Integer setmealId = setmeal.getId();
        //将检查组id与套餐id关联到t_setmeal_checkgroup表
        for (Integer checkgroupId : checkgroupIds) {
            setmealCheckgroupMapper.add(setmealId,checkgroupId);
        }
    }

    /**
     * 编辑套餐
     * @param checkgroupIds
     * @param setmeal
     */
    public void edit(List<Integer> checkgroupIds, Setmeal setmeal) {
        Setmeal oldSetmeal = setmealMapper.getById(setmeal.getId());//获取旧数据
        String oldImage = oldSetmeal.getImg();
        //修改套餐表中的数据
        setmealMapper.edit(setmeal);
        //根据套餐id删除套餐项目组关系表中的数据
        if (checkgroupIds!= null) {
            setmealCheckgroupMapper.deleteBySetmealId(setmeal.getId());
            //将检查组id与套餐id关联到t_setmeal_checkgroup表
            for (Integer checkgroupId : checkgroupIds) {
                setmealCheckgroupMapper.add(setmeal.getId(),checkgroupId);
            }
        }
        // 处理新图片
        moveImageToCommitted(setmeal.getImg());
        // 删除旧图片
        if (oldImage != null && !oldImage.equals(setmeal.getImg())) {
            deleteOldImage(oldImage);
        }
    }

    /**
     * 根据id查找套餐
     * @param id
     * @return
     */
    public Setmeal findById(Integer id) {
        return setmealMapper.getById(id);
    }

    /**
     * 分页查询套餐
     * @param queryPageBean
     * @return
     */
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Setmeal> setmealPage = setmealMapper.findPage(queryPageBean.getQueryString());
        return new PageResult(setmealPage.getTotal(),setmealPage.getResult());
    }

    /**
     *  将上传的图片移动到已提交的图片集合中
     * @param imagePath 图片路径
     */
    private void moveImageToCommitted(String imagePath) {
        // 如果图片路径不为空
        if (imagePath != null && !imagePath.isEmpty()) {
            // 将图片从上传的图片集合中移动到已提交的图片集合中
            redisTemplate.opsForSet().move("uploadedImages", imagePath, "committedImages");
        }
    }

    /**
     *  删除旧图片
     * @param oldImage 旧图片的路径
     */
    private void deleteOldImage(String oldImage) {
        try {
            // 从Redis中移除
            redisTemplate.opsForSet().remove("committedImages", oldImage);
            // 删除OSS文件
            aliOssUtil.delete(oldImage);
        } catch (Exception e) {
            log.error("删除旧图片失败：{}", oldImage, e);
        }
    }
}
